package com.boang.controller;

import com.boang.entity.Film;
import com.boang.service.IFilmService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.Year;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hxao
 * @since 2025-02-19
 */
@RestController
@RequestMapping("/film")
@Slf4j
public class FilmController {
    @Autowired
    private IFilmService filmService;

    private Film createFilm(){
        Film film = new Film();
        // 设置假数据
        film.setTitle("The Great Adventure");
        film.setDescription("An epic adventure across the seas.");
        film.setReleaseYear(Year.of(2022));
        film.setLanguageId(1); // 假设语言 ID 为 1
        film.setOriginalLanguageId(1); // 假设原语言 ID 为 1
        film.setRentalDuration(7); // 租赁持续时间为 7 天
        film.setRentalRate(new BigDecimal("2.99"));
        film.setLength(120); // 长度为 120 分钟
        film.setReplacementCost(new BigDecimal("19.99"));
        film.setRating("PG");
        film.setSpecialFeatures("Deleted scenes");
        film.setLastUpdate(LocalDateTime.now());
        return film;
    }

    @PostMapping
    public void save(){
        Film film = createFilm();
        boolean save = filmService.save(film);
        log.info("新增结果：{}", save);
    }

    @PutMapping
    public void update(){
        Film film = createFilm();
        film.setFilmId(2);
        film.setTitle("qwqwq");
        boolean b = filmService.saveOrUpdate(film);
        log.info("修改结果：{}", b);
    }

    @DeleteMapping
    public void del(){
        boolean b = filmService.removeById(2);
        log.info("执行删除操作，结果：{}", b);
    }

    @GetMapping("/{id}")
    public void getById(@PathVariable Integer id){
        Film film = filmService.getById(id);
        log.info("查询结果：{}", film);
    }

    @GetMapping("/ids")
    public void getBatchByIds(@RequestParam List<Integer> ids){
        List<Film> filmList = filmService.listByIds(ids);
        log.info("批量查询结果：{}", filmList);
    }

    @GetMapping
    public void getAll(){
        List<Film> list = filmService.list();
        log.info("查询全部结果：{}", list);
    }

}
